import React, { useReducer, useEffect } from 'react'
import { AsyncStorage } from 'react-native'
import pos, { PosAppGetinIuput, HttpResult } from 'api/pos'
import { PosModule } from 'native-modules'
import Loading from 'components/loading';

interface PayInfo {
	orderInfo: PosAppGetinIuput
	umsInfo?: string
}

interface PayContext {
	allPayData: PayInfo[]
	addOrderData: (data: PosAppGetinIuput) => void
	sendData: (data: PayInfo) => Promise<HttpResult>
	sendDataTest: (data: PayInfo) => void
	sendAllData: () => void
	removeData: (data: PayInfo) => void
	refreshAll: () => void
	cleanData: () => void
}

const PAY_KEY = 'pay-data-key'

let payContext = { allPayData: [] } as PayContext

const PayContext = React.createContext(payContext)
export const usePayContext = () => React.useContext(PayContext)

export const PayProvide: React.FC = props => {
	const [payData, setPayData] = React.useState<PayInfo[]>([])

	useEffect(() => {
		AsyncStorage.getItem(PAY_KEY).then(res => {
			let data = res ? JSON.parse(res) : []
			setPayData(data)
		})
	}, [])

	useEffect(() => {
		AsyncStorage.setItem(PAY_KEY, JSON.stringify(payData))
	}, [payData])

	const cleanData = () => {
		setPayData([])
	}

	//添加订单数据
	const addOrderData = (data: PosAppGetinIuput) => {
		payData.push({ orderInfo: data })
		setPayData([...payData])
	}

	const sendData = (data: PayInfo): Promise<HttpResult> => {
		//调用接口去发送data请求到后台并返回调用接口
		let newData = payData.map(res => {
			if (res.orderInfo.chinaPayRefId === data.orderInfo.chinaPayRefId) {
				return data
			} else {
				return res
			}
		})
		setPayData(newData)
		return postData(data)
	}

	const sendDataTest = (data: PayInfo): void => {
		console.log('11', payData)
		//调用接口去发送data请求到后台并返回调用接口
		let newData = payData.map(res => {
			if (res.orderInfo.chinaPayRefId === data.orderInfo.chinaPayRefId) {
				return data
			} else {
				return res
			}
		})
		console.log('22', newData, data)
		setPayData(newData)
	}

	const sendAllData = () => {
		Loading.show()
		payData.forEach(item => {
			postData(item)
		})
		Loading.hide()
	}

	//外部调用refreshAll不需要传data参数
	const refreshAll = () => {
		payData.forEach(item => {
			if (!item.umsInfo && item.umsInfo === '') {
				//查询pos数据
				PosModule.queryOrder(item.orderInfo.chinaPayRefId).then(result => {
					//TODO 数据查询到了去更新payData数据
				})
			}
		})
		//TODO 去更新数据
	}

	//将数据发送到后台
	const postData = (data: PayInfo): Promise<HttpResult> => {

		return pos.posApp.handlePaySuccess({ ...data.orderInfo, bankInfo: data.umsInfo }).then(res => {
			//数据同步完成移除数据
			removeData(data)
			return res
		})
	}

	//sendData,refreshAll 调用后台接口成功后需要调用此方法去remove数据 ,
	const removeData = (data: any) => {
		//根据业务逻辑将data数据从payData中移除
		let realData = payData.filter(item => item.orderInfo.chinaPayRefId != data.orderInfo.chinaPayRefId)
		setPayData(realData)
	}

	payContext = { allPayData: payData, addOrderData, sendData, sendDataTest, sendAllData, refreshAll, removeData, cleanData }
	return <PayContext.Provider {...props} value={payContext} />
}
